package cn.lsngo.manager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import cn.lsngo.manager.greendao.ElectricBean;
import cn.lsngo.manager.greendao.gen.ElectricBeanDao;

/**
 * @author 1
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private CommonItemView mCivWaterTotal;
    private double electricalFee = 0.55;
    private String mA;
    private String mB;
    private String mC;
    private String mD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView mTvTitle = (TextView) findViewById(R.id.tv_title);
        findViewById(R.id.tv_confirm).setOnClickListener(this);
        findViewById(R.id.tv_history).setOnClickListener(this);

        mTvTitle.setText(String.format("2022-2.18电费详情\n当前电单价：%s元/度", electricalFee));

        CommonItemView mCivA = (CommonItemView) findViewById(R.id.civ_a);
        CommonItemView mCivB = (CommonItemView) findViewById(R.id.civ_b);
        CommonItemView mCivC = (CommonItemView) findViewById(R.id.civ_c);
        CommonItemView mCivD = (CommonItemView) findViewById(R.id.civ_d);
        CommonItemView mCivElectricTotal = (CommonItemView) findViewById(R.id.civ_electric_total);
        TextView mTvCalculate = (TextView) findViewById(R.id.tv_calculate);
        mCivWaterTotal = (CommonItemView) findViewById(R.id.civ_total);
        mCivElectricTotal.setEndVisible(false);
        mCivElectricTotal.setHintContent("请输入总金额");

        TextView mTvWaterCalculate = (TextView) findViewById(R.id.tv_water_calculate);
        mTvWaterCalculate.setOnClickListener(v -> {
            String content = mCivWaterTotal.getContentStart();
            double str = ArithUtil.parseStr2Double(content) / 4;
            mCivWaterTotal.setHint(str + "");
        });
        mTvCalculate.setOnClickListener(v -> {
            double mTotalMoney = ArithUtil.parseStr2Double(mCivElectricTotal.getContentStart());

            if (TextUtils.isBlank(mCivA.getContentStart())
                    && TextUtils.isBlank(mCivB.getContentStart())
                    && TextUtils.isBlank(mCivC.getContentStart())
                    && TextUtils.isBlank(mCivD.getContentStart())) {
                double mEverybodyMoney = mTotalMoney / 4;
                mCivA.setHint(String.format("%s/4=%s", mTotalMoney, mEverybodyMoney));
                mCivB.setHint(String.format("%s/4=%s", mTotalMoney, mEverybodyMoney));
                mCivC.setHint(String.format("%s/4=%s", mTotalMoney, mEverybodyMoney));
                mCivD.setHint(String.format("%s/4=%s", mTotalMoney, mEverybodyMoney));
                return;
            }
            double v2A = 0;
            double v2B = 0;
            double v2C = 0;
            double v2D = 0;
            double v1A = 0;
            double v1B = 0;
            double v1C = 0;
            double v1D = 0;
            if (!TextUtils.isBlank(mCivA.getContentStart())) {
                // 计算a房的空调费用
                v1A = ArithUtil.sub(mCivA.getContentStart(), mCivA.getContentEnd());
                v2A = v1A * electricalFee;
            }
            if (!TextUtils.isBlank(mCivB.getContentStart())) {
                v1B = ArithUtil.sub(mCivB.getContentStart(), mCivB.getContentEnd());
                v2B = v1B * electricalFee;
            }
            if (!TextUtils.isBlank(mCivC.getContentStart())) {
                v1C = ArithUtil.sub(mCivC.getContentStart(), mCivC.getContentEnd());
                v2C = v1C * electricalFee;
            }
            if (!TextUtils.isBlank(mCivD.getContentStart())) {
                v1D = ArithUtil.sub(mCivD.getContentStart(), mCivD.getContentEnd());
                v2D = v1D * electricalFee;
            }
            // 除去电费还需付费
            double mTotal = mTotalMoney - v2A - v2B - v2C - v2D;
            double mEverybodyMoney = ArithUtil.remain2dot(mTotal / 4);
            mA = String.format("%s*%s+%s= %s", v1A, electricalFee, mEverybodyMoney, ArithUtil.remain2dot(mEverybodyMoney + v2A));
            mCivA.setHint(mA);
            mB = String.format("%s*%s+%s= %s", v1B, electricalFee, mEverybodyMoney, ArithUtil.remain2dot(mEverybodyMoney + v2B));
            mCivB.setHint(mB);
            mC = String.format("%s*%s+%s= %s", v1C, electricalFee, mEverybodyMoney, ArithUtil.remain2dot(mEverybodyMoney + v2C));
            mCivC.setHint(mC);
            mD = String.format("%s*%s+%s= %s", v1D, electricalFee, mEverybodyMoney, ArithUtil.remain2dot(mEverybodyMoney + v2D));
            mCivD.setHint(mD);
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_confirm:
                ElectricBeanDao electricBeanDao = DbManager.getInstance().getDaoSession().getElectricBeanDao();
                electricBeanDao.insert(getElectricBean());
                break;
            case R.id.tv_history:
                Intent intent = new Intent(MainActivity.this, HistoryListActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private ElectricBean getElectricBean() {
        ElectricBean electricBean = new ElectricBean();
        electricBean.setCreateTime(DateUtil.getCurrentTime());
        electricBean.setUpdateTime(DateUtil.getCurrentTime());
        electricBean.setList(getList());
        return electricBean;
    }

    private List<ElectricDetailBean> getList() {
        List<ElectricDetailBean> list = new ArrayList<>();
        ElectricDetailBean aBean = new ElectricDetailBean();
        aBean.setRoomNumber("A");
        aBean.setDetail(mA);
//        aBean.setTotalMoney();
//        aBean.setRoomMoney();
        list.add(aBean);

        ElectricDetailBean bBean = new ElectricDetailBean();
        bBean.setRoomNumber("B");
        bBean.setDetail(mB);
//        bBean.setTotalMoney();
//        bBean.setRoomMoney();
        list.add(bBean);

        ElectricDetailBean cBean = new ElectricDetailBean();
        cBean.setRoomNumber("C");
        cBean.setDetail(mC);
//        cBean.setTotalMoney();
//        cBean.setRoomMoney();
        list.add(cBean);

        ElectricDetailBean dBean = new ElectricDetailBean();
        dBean.setRoomNumber("D");
        dBean.setDetail(mD);
//        dBean.setTotalMoney();
//        dBean.setRoomMoney();
        list.add(dBean);

        return list;
    }
}